package tree;

import java.util.Stack;

/**
 * 112. 路径总和
 *
 * @author Api
 * @date 2023/10/23 22:57
 */
public class Code112_PathSummation {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    // 卡尔
    public boolean hasPathSum2(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        return traversal(root, targetSum - root.val);
    }

    private boolean traversal(TreeNode node, int count) {
        // 终止条件
        if (node.left == null && node.right == null && count == 0) {
            return true;
        }
        if (node.left == null && node.right == null && count != 0) {
            return false;
        }
        // 左
        if (node.left != null) {
            count -= node.left.val;
            if (traversal(node.left, count)) {
                return true;
            }
            count += node.left.val; // 回溯
        }
        // 右
        if (node.right != null) {
            count -= node.right.val;
            if (traversal(node.right, count)) {
                return true;
            }
            count += node.right.val; // 回溯
        }
        return false;
    }

    // 递归法（前中后序都可以）
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        targetSum -= root.val;
        // 叶子节点
        if (root.left == null && root.right == null) {
            return targetSum == 0;
        }
        if (root.left != null) {
            boolean left = hasPathSum(root.left, targetSum);
            if (left) { // 已经找到
                return true;
            }
        }
        if (root.right != null) {
            boolean right = hasPathSum(root.right, targetSum);
            if (right) { // 已经找到
                return true;
            }
        }
        return false;
    }

    // 简洁方法
    public boolean hasPathSum1(TreeNode root, int targetSum) {
        if (root == null) {
            return false; // 为空退出
        }
        // 叶子节点判断是否符合
        if (root.left == null && root.right == null) {
            return root.val == targetSum;
        }
        // 求两侧分支的路径和
        return hasPathSum1(root.left, targetSum - root.val) || hasPathSum1(root.right, targetSum - root.val);
    }

    // 迭代法
    public boolean hasPathSum3(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        stack1.push(root);
        stack2.push(root.val);
        while (!stack1.isEmpty()) {
            int size = stack1.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = stack1.pop();
                int sum = stack2.pop();
                // 如果该节点时叶子节点了，同时该节点的路径数值等于sum，那么就返回true
                if (node.left == null && node.right == null && sum == targetSum) {
                    return true;
                }
                //右节点，压进去一个节点的时候，将该节点的路径数值也记录下来
                if (node.right != null) {
                    stack1.push(node.right);
                    stack2.push(sum + node.right.val);
                }
                // 左节点，压进去一个节点的时候，将该节点的路径数值也记录下来
                if (node.left != null) {
                    stack1.push(node.left);
                    stack2.push(sum + node.left.val);
                }
            }
        }
        return false;
    }
}
